#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    vector<double> averageOfLevels(TreeNode* root) {
        queue<TreeNode*> q;
        vector<double> ans;
        q.push(root);
        while (!q.empty())
        {
            int size = q.size();
            double sum = 0;
            for (int i = 0; i < size; i++)
            {
                TreeNode* top = q.front();
                q.pop();
                sum += top->val;
                if (top->left) q.push(top->left);
                if (top->right) q.push(top->right);
            }
            ans.push_back(sum / size);
        }
        return ans;
    }
};